package net.dgg.zz.production.chargeback.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.production.chargeback.service.ZzAuditOrderServic;
import net.dgg.zz.production.chargeback.service.ZzChargeBcakService;
import net.dgg.zz.production.vo.ChargeBackOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 孙上峰
 * @ClassName 已退单<ZzChargeBcakController>
 * @despriction
 * @create 2018/08/17 10:33
 **/
@Controller
@RequestMapping(value = "/zzsc/chargebcak")
public class ZzChargeBcakController extends DggBaseController {

    @Autowired
    protected DesUtil desUtil;
    @Autowired
    protected CommonMangerExtService zzCommonMangerExtService;
    @Autowired
    protected SessionManager sessionManager;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private ZzChargeBcakService zzChargeBcakService;
    @Autowired
    private ZzAuditOrderServic zzAuditOrderServic;

    /**
     * 初始化界面
     */
    @RequestMapping(value = "/index")
    public String index(Model model) {
        return "zzsc/auditOrder/page_order_abandoned";
    }

    /**
     * 已退单列表
     * 孙上峰
     *
     * @param params {codeName:模块名称}
     * @return
     */
    @RequestMapping(value = "/query_audit_list")
    @ResponseBody
    public DataTableResponse queryAuditList(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        UserEntity userEntity = sessionManager.getCurrentUser();
        params.put("start", r.getStart());
        params.put("limit", r.getLength());
        params.put("userEntity", userEntity.getId()); //当前登录人auditorId
        params.put("status", ScProductConstant.SC_ORDER_FINISHED_CHARGEBACK);//已退单
        if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(params.get("customerPhone"))) {
            params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
        }
        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> listPage = zzChargeBcakService.queryAuditListPage(params);
        String[] transColum = {"originCode", "originType", "status", "businessTypeId", "businessProductId", "abandonReason"};
        listPage = translateUtil.translateList(transColum, listPage);
        String[] hideColum = {"customerPhone"};
        listPage = desUtil.decryptListMap(hideColum, listPage);
        CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
        Integer count = Integer.parseInt(params.get("count").toString());
        return new DataTableResponse(r.getDraw(), count, count, listPage, "");
    }

    /**
     * 导出
     *
     * @param params
     * @return
     */
    @RequestMapping("export_sczz_abandoned")
    public void exportSczzAbandoned(@RequestParam Map params, HttpServletResponse response) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            params.put("userEntity", userEntity.getId()); //当前登录人auditorId
            if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(params.get("customerPhone"))) {
                params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
            }
            String fileName = "已退单_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            params.put("status", ScProductConstant.SC_ORDER_FINISHED_CHARGEBACK);
            List<ChargeBackOrderVo> dataList = zzChargeBcakService.findByParamsList(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response,ChargeBackOrderVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
